package com.huluobo.a2112mvvmdouyindemo.shop;

import androidx.lifecycle.MutableLiveData;

import com.huluobo.mvvm_base.bean.AddToCartBeanResult;
import com.huluobo.mvvm_base.bean.GoodsBeanResult;
import com.huluobo.mvvm_base.model.BaseRepo;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

/**
 * Created by LC on 2024/4/9.
 */
public class ShopRepo extends BaseRepo {
    public void getGoodsList(int categoryId, int currentPage, int pageSize, MutableLiveData<GoodsBeanResult> success,
                             MutableLiveData<String> failed) {
        apiService.getGoodsList(categoryId, currentPage, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GoodsBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(GoodsBeanResult goodsBeanResult) {
                        success.setValue(goodsBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void addToCart(RequestBody body, MutableLiveData<AddToCartBeanResult> success,
                          MutableLiveData<String> failed) {
        apiService.addToCart(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AddToCartBeanResult>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(AddToCartBeanResult addToCartBeanResult) {
                        success.setValue(addToCartBeanResult);
                    }

                    @Override
                    public void onError(Throwable e) {
                        failed.setValue(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
